package lium.lbs.lbsim.zfw

import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.buz.hjcdriver.bean.AResultBean
import com.lmlibrary.Constants
import com.lmlibrary.bean.ResponseBean
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_toadd_friend.*
import lium.lbs.lbsim.activity.BaseActivity
import lium.lbs.lbsim.utils.callbck.DialogCallback
import lium.lbs.lbsim.R
import lium.lbs.lbsim.bean.FriendInfoBean
import lium.lbs.lbsim.utils.SpUtils

class PotentialFriendActivity : BaseActivity() {

    var fid: String? = null

    override fun getLayoutId(): Int {
        return R.layout.activity_toadd_friend
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitleWithBack("")
        fid = intent.getStringExtra("fid")
        addOnClickListeners(R.id.contacts_add)
    }

    override fun getNetWorkData() {
        getDataList()
    }

    fun getDataList() {
        val param = HashMap<String, String>()
        param["mid"] = SpUtils.getInstance().get(Constants.memberId, "") as String
        param["fid"] = fid.toString()
        postData(
            "/frinds/frindInfo",
            param,
            object : DialogCallback<ResponseBean<FriendInfoBean>>(this) {
                override fun onSuccess(response: Response<ResponseBean<FriendInfoBean>>) {
                    setDataList(response.body().data)
                }
            })
    }

    fun setDataList(list: FriendInfoBean) {
        displayImage(Constants.Host_Pic + list?.friend.headImage, R.drawable.icon_defalut_head, contacts_head)
        contacts_name.text = list?.friend?.nickName
        contacts_dec.text = list?.friend?.signature
        contacts_where.text = list?.friend?.province + list?.friend?.city
        contacts_code.text = list?.friend?.uniqueCode
        if (list?.friend?.isFriend.equals("1")) {//1是朋友
            contacts_add.visibility = View.GONE
        } else {
            if (fid.equals(SpUtils.getInstance().get(Constants.memberId, "") as String)) {
                contacts_add.visibility = View.GONE
            }
        }
    }

    fun addFriend() {
        val param = HashMap<String, String>()
        param["mid"] = SpUtils.getInstance().get(Constants.memberId, "") as String
        param["fid"] = fid.toString()//1：找人；2：找群
        param["applyInfo"] = SpUtils.getInstance().get(Constants.nickName, "") as String + "请求添加好友"//1：找人；2：找群
        postData(
            "/frinds/frindadd",
            param,
            object : DialogCallback<ResponseBean<AResultBean>>(this) {
                override fun onSuccess(response: Response<ResponseBean<AResultBean>>) {
                    setAddFriend(response.body().msg)
                }
            })
    }


    fun setAddFriend(msg: String) {
        Toast.makeText(this@PotentialFriendActivity, msg, Toast.LENGTH_SHORT).show()
        finish()
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v!!.id) {
            R.id.contacts_add -> {
                addFriend()
            }

        }
    }
}
